﻿//--Create by LacHV

using System;
using System.Data;
using System.Data.SqlClient;
using Pantry.Commons;
using Pantry.Entity;
using System.Globalization;

namespace Pantry.DataAccess
{
    public class ProgramDao:PantryDao
    {
        public ProgramDao()
        {
        }

        public ProgramDao(SqlConnection sqlConnection)
            : base(sqlConnection)
        {
        }

        public override DataTable RetrieveAllData()
        {
            return RetrieveData(DBSchemas.SqlRetrieveAllProgramDataTable);
        }

        public override object RetrieveEntity(int id)
        {
            if (id < 1)
            {
                return null;
            }

            var parameters = new SqlParameter("@ProgramId", SqlDbType.Int) { Value = id };
            var dataTable = RetrieveData(DBSchemas.SqlRetrieveProgram, parameters);
            if (dataTable == null)
            {
                return null;
            }

            var dataRow = dataTable.Rows[0];
            var program = new Program
            {
                ProgramId = Convert.ToInt32(dataRow["ProgramId"], CultureInfo.CurrentCulture),
                ProgramName = dataRow["ProgramName"].ToString(),
                Description = dataRow["Description"].ToString(),
                IsActive = Convert.ToBoolean(dataRow["IsActive"], CultureInfo.CurrentCulture),
                ContactId = Convert.ToInt32(dataRow["ContactId"], CultureInfo.CurrentCulture),
                ContactName = dataRow["ContactName"].ToString(),
                OrganizationId = Convert.ToInt32(dataRow["OrganizationId"], CultureInfo.CurrentCulture)
            };
            return program;
        }

        public override int Insert(object sender)
        {
            var program = (Program)sender;
            if (program == null)
                return 0;

            var result = CheckUnique(@"Program", @"ProgramName", program.ProgramName);
            if (result > 0)
            {
                return -2;
            }
                   
            var parameters = new SqlParameter[5];
            parameters[0] = new SqlParameter("@ProgramName", SqlDbType.VarChar, 100) { Value = program.ProgramName };
            parameters[1] = new SqlParameter("@Description", SqlDbType.VarChar, 400) { Value = program.Description };
            parameters[2] = new SqlParameter("@IsActive", SqlDbType.Bit) { Value = program.IsActive };
            parameters[3] = new SqlParameter("@ContactId", SqlDbType.Int) { Value = program.ContactId };
            parameters[4] = new SqlParameter("@OrganizationId", SqlDbType.Int) { Value = program.OrganizationId };
            return ExecuteNonQuery(DBSchemas.SqlInsertProgram, parameters);
        }

        public override int Update(object sender)
        {
            var program = (Program)sender;
            if (program == null)
            {
                return 0;
            }

            var result = CheckUniqueForAmend(@"Program", @"ProgramName", program.ProgramName, @"ProgramId", program.ProgramId);
            if (result != 0)
            {
                return -2;
            }

            var parameters = new SqlParameter[5];
            parameters[0] = new SqlParameter("@ProgramName", SqlDbType.VarChar, 100) { Value = program.ProgramName };
            parameters[1] = new SqlParameter("@Description", SqlDbType.VarChar, 400) { Value = program.Description };
            parameters[2] = new SqlParameter("@ContactId", SqlDbType.Int) { Value = program.ContactId };
            parameters[3] = new SqlParameter("@OrganizationId", SqlDbType.Int) { Value = program.OrganizationId };
            parameters[4] = new SqlParameter("@ProgramId", SqlDbType.Int) { Value = program.ProgramId };
            return ExecuteNonQuery(DBSchemas.SqlUpdateProgram, parameters);
        }

    }
}